package com.javaee.controller.user;

import com.javaee.entity.Department;
import com.javaee.entity.Doctor;
import com.javaee.entity.User;
import com.javaee.service.doctor.AuthDoctorService;
import com.javaee.service.user.AuthUserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.util.List;

@Controller
@RequestMapping("/auth")
public class AuthController {
    @Autowired
    private AuthUserService authUserService;

    @Autowired
    private AuthDoctorService authDoctorService;

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "user/login";
    }
    @RequestMapping("/login")
    public String login(HttpServletRequest request, HttpSession session, Model model) {
        String phone = request.getParameter("phone");
        String password = request.getParameter("password");
        String role = request.getParameter("role");

        if ("patient".equals(role)) {
            User user = authUserService.login(phone, password);
            if (user != null) {
                session.setAttribute("User", user);
                return "redirect:/user/appointment";
//                return "user/userindex";
            } else {
                model.addAttribute("msg", "登录失败，请检查用户名和密码。");
                return "/user/login";
            }
        } else if ("doctor".equals(role)) {
            Doctor doctor = authDoctorService.login(phone, password);
            if (doctor != null) {
                session.setAttribute("Doctor", doctor);
                return "redirect:/lastedBoardList";
//                return "redirect:/user/doctorindex";
            } else {
                model.addAttribute("msg", "登录失败，请检查用户名和密码。");
                return "user/login";
            }
        } else {
            model.addAttribute("msg", "请选择有效的身份。");
            return "user/login";
        }
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/";
    }

    @RequestMapping("/toRegister")
    public String toRegister(Model model){

        List<Department> departmentList = authUserService.selectAllDepartments();
        model.addAttribute("departments", departmentList);
        return "user/register";
    }
    @RequestMapping("/register")
    public String register(HttpServletRequest request, HttpSession session, Model model, MultipartFile doctorPhoto,MultipartFile patientPhoto) {
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String identity = request.getParameter("identity");

        if ("patient".equals(identity)) {
            // 获取患者特有字段
//            String name = request.getParameter("patient_name");
            String sex = request.getParameter("sex");
            String address = request.getParameter("address");
            String birthdayStr = request.getParameter("birthday");
            String cno = request.getParameter("cno");
            String phone = request.getParameter("patient_phone");
            LocalDate birthday = (birthdayStr != null && !birthdayStr.isEmpty()) ? LocalDate.parse(birthdayStr) : null;

            // 调用患者注册服务
            authUserService.registerPatient( password,name, sex, address, birthday,cno,phone,patientPhoto);
            model.addAttribute("msg", "患者注册成功，请登录。");
        } else if ("doctor".equals(identity)) {
            // 获取医生特有字段
//            String name = request.getParameter("doctor_name");
            String phone = request.getParameter("doctor_phone");
            String job = request.getParameter("job");
            String fee = request.getParameter("fee");
            String note = request.getParameter("note");
            int dName = Integer.parseInt(request.getParameter("dName"));
            System.out.println("科室:"+dName);

            // 调用医生注册服务
            authDoctorService.registerDoctor( password, phone, name,job,note, Double.parseDouble(fee),dName,doctorPhoto);

            model.addAttribute("msg", "医生注册成功，请登录。");
        } else {
            model.addAttribute("msg", "请选择有效的身份。");
            return "user/register"; // 返回注册页面并显示错误信息
        }

        // 注册成功后跳转到登录页面
        return "redirect:/auth/login";
    }
}